package com.hdc.mumuwei.micro_account_server.service.impl;

import com.hdc.mumuwei.micro_account_server.mapper.BillMapper;
import com.hdc.mumuwei.micro_account_server.po.Bill;
import com.hdc.mumuwei.micro_account_server.po.Paging;
import com.hdc.mumuwei.micro_account_server.service.BillService;
import com.hdc.mumuwei.micro_account_server.utils.Result;
import com.hdc.mumuwei.micro_account_server.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mumuwei
 * @Description
 * @create 2021/11/19 22:47
 */
@Service
public class BillServiceImpl implements BillService {

    @Autowired
    BillMapper billMapper;

    Result result;

    /**
     * 获取账单列表（分页）
     *
     * @param paging
     */
    @Override
    public Result getBillList(Paging paging) {
        Map<String, Object> data = new HashMap();
        paging.setStart((paging.getCurrent()-1)* paging.getSize());
        int total = billMapper.getBillTotal(paging);
        data.put("total", total);
        List<Bill> list = billMapper.getBillList(paging);
        data.put("list", list);
        if(list!= null){
            result = new Result<>(data);
        }else {
            result = new Result<>(Result.ERR_CODE,Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 获取账单列表（不分页）
     *
     * @param paging paging对象
     * @return
     */
    @Override
    public Result getAllBills(Paging paging) {
        Map<String, Object> data = new HashMap();
        int total = billMapper.getBillTotal(paging);
        data.put("total", total);
        List<Bill> list = billMapper.getAllBills(paging);
        data.put("list", list);
        if(list!= null){
            result = new Result<>(data);
        }else {
            result = new Result<>(Result.ERR_CODE,Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 根据用户id和账单id获取账单信息
     *
     * @param bill 账单信息
     * @return 账单信息
     */
    @Override
    public Result getBillById(Bill bill) {
        Bill bill1 = billMapper.getBillById(bill);
        if(bill1 != null){
            result = new Result(Result.SUCCESS_CODE, Result.SUCCESS_MSG, bill1);
        }else {
            result = new Result(Result.ERR_CODE, Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 添加账单
     *
     * @param bill 账单对象
     * @return
     */
    @Override
    public Result addBill(Bill bill) {
        bill.setCreateTime(TimeUtils.getCurrentTimes());
        int row = billMapper.addBill(bill);
        if(row>0){
            result = new Result<>(Result.SUCCESS_CODE, Result.ADD_OK_MSG);
        }else {
            result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 更新账单
     *
     * @param bill 账单信息
     * @return
     */
    @Override
    public Result updateBill(Bill bill) {
        bill.setUpdateTime(TimeUtils.getCurrentTimes());
        int row = billMapper.updateBill(bill);
        if(row>0){
            result = new Result<>(Result.SUCCESS_CODE, Result.UPDATE_OK_MSG);
        }else {
            result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
        }
        return result;
    }

    /**
     * 删除账单
     *
     * @param bill 用户uid 和 账单bid
     * @return
     */
    @Override
    public Result delBill(Bill bill) {
        int row = billMapper.delBill(bill);
        if(row>0){
            result = new Result<>(Result.SUCCESS_CODE, Result.DELETE_OK_MSG);
        }else {
            result = new Result<>(Result.ERR_CODE, Result.ERR_MSG);
        }
        return result;
    }
}
